package com.tgy.leetcode;

/**
 * @Author: tgy
 * @Date: 12/5/20 10:20 AM
 *
 * https://leetcode-cn.com/problems/subtree-of-another-tree/
 */
public class _572_另一个树的子树 {

    public boolean isSubtree(TreeNode s, TreeNode t) {

        if (s == null || t == null) {

            return false;
        }

        String s1 = serializeTree(s);
        String s2 = serializeTree(t);
        return s1.contains(s2);
    }

    private String serializeTree(TreeNode node) {

        StringBuilder sb = new StringBuilder();

        traversalTree(node,sb);
        return sb.toString();
    }


    private void traversalTree(TreeNode node,StringBuilder sb)  {

        if (node == null) {

            sb.append("#!");
            return;
        }

        traversalTree(node.left,sb);
        traversalTree(node.right,sb);
        sb.append(node.val).append("!");
    }

}
